{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c4918310-bc9a-4dec-9da3-d7b4f82cccd2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始数据集划分\n",
      "*********************************Blight*************************************\n",
      "Blight类按照0.8：0.0：0.2的比例划分完成，一共1146张图片\n",
      "训练集../dataset/CornDataset/data/train/Blight：917张\n",
      "验证集../dataset/CornDataset/data/val/Blight：0张\n",
      "测试集../dataset/CornDataset/data/test/Blight：229张\n",
      "*********************************Common_Rust*************************************\n",
      "Common_Rust类按照0.8：0.0：0.2的比例划分完成，一共1306张图片\n",
      "训练集../dataset/CornDataset/data/train/Common_Rust：1045张\n",
      "验证集../dataset/CornDataset/data/val/Common_Rust：0张\n",
      "测试集../dataset/CornDataset/data/test/Common_Rust：261张\n",
      "*********************************Gray_Leaf_Spot*************************************\n",
      "Gray_Leaf_Spot类按照0.8：0.0：0.2的比例划分完成，一共574张图片\n",
      "训练集../dataset/CornDataset/data/train/Gray_Leaf_Spot：460张\n",
      "验证集../dataset/CornDataset/data/val/Gray_Leaf_Spot：0张\n",
      "测试集../dataset/CornDataset/data/test/Gray_Leaf_Spot：114张\n",
      "*********************************Healthy*************************************\n",
      "Healthy类按照0.8：0.0：0.2的比例划分完成，一共1162张图片\n",
      "训练集../dataset/CornDataset/data/train/Healthy：930张\n",
      "验证集../dataset/CornDataset/data/val/Healthy：0张\n",
      "测试集../dataset/CornDataset/data/test/Healthy：232张\n"
     ]
    }
   ],
   "source": [
    "# 工具类\n",
    "import os\n",
    "import random\n",
    "import shutil\n",
    "from shutil import copy2\n",
    "\n",
    "\n",
    "def data_set_split(src_data_folder, target_data_folder, train_scale=0.8, val_scale=0.0, test_scale=0.2):\n",
    "    '''\n",
    "    读取源数据文件夹，生成划分好的文件夹，分为trian、val、test三个文件夹进行\n",
    "    :param src_data_folder: 源文件夹 E:/biye/gogogo/note_book/torch_note/data/utils_test/data_split/src_data\n",
    "    :param target_data_folder: 目标文件夹 E:/biye/gogogo/note_book/torch_note/data/utils_test/data_split/target_data\n",
    "    :param train_scale: 训练集比例\n",
    "    :param val_scale: 验证集比例\n",
    "    :param test_scale: 测试集比例\n",
    "    :return:\n",
    "    '''\n",
    "    print(\"开始数据集划分\")\n",
    "    class_names = os.listdir(src_data_folder)\n",
    "    # 在目标目录下创建文件夹\n",
    "    split_names = ['train', 'val', 'test']\n",
    "    for split_name in split_names:\n",
    "        split_path = os.path.join(target_data_folder, split_name)\n",
    "        if os.path.isdir(split_path):\n",
    "            pass\n",
    "        else:\n",
    "            os.mkdir(split_path)\n",
    "        # 然后在split_path的目录下创建类别文件夹\n",
    "        for class_name in class_names:\n",
    "            class_split_path = os.path.join(split_path, class_name)\n",
    "            if os.path.isdir(class_split_path):\n",
    "                pass\n",
    "            else:\n",
    "                os.mkdir(class_split_path)\n",
    "\n",
    "    # 按照比例划分数据集，并进行数据图片的复制\n",
    "    # 首先进行分类遍历\n",
    "    for class_name in class_names:\n",
    "        current_class_data_path = os.path.join(src_data_folder, class_name)\n",
    "        current_all_data = os.listdir(current_class_data_path)\n",
    "        current_data_length = len(current_all_data)\n",
    "        current_data_index_list = list(range(current_data_length))\n",
    "        random.shuffle(current_data_index_list)\n",
    "\n",
    "        train_folder = os.path.join(os.path.join(target_data_folder, 'train'), class_name)\n",
    "        val_folder = os.path.join(os.path.join(target_data_folder, 'val'), class_name)\n",
    "        test_folder = os.path.join(os.path.join(target_data_folder, 'test'), class_name)\n",
    "        train_stop_flag = current_data_length * train_scale\n",
    "        val_stop_flag = current_data_length * (train_scale + val_scale)\n",
    "        current_idx = 0\n",
    "        train_num = 0\n",
    "        val_num = 0\n",
    "        test_num = 0\n",
    "        for i in current_data_index_list:\n",
    "            src_img_path = os.path.join(current_class_data_path, current_all_data[i])\n",
    "            if current_idx <= train_stop_flag:\n",
    "                copy2(src_img_path, train_folder)\n",
    "                # print(\"{}复制到了{}\".format(src_img_path, train_folder))\n",
    "                train_num = train_num + 1\n",
    "            elif (current_idx > train_stop_flag) and (current_idx <= val_stop_flag):\n",
    "                copy2(src_img_path, val_folder)\n",
    "                # print(\"{}复制到了{}\".format(src_img_path, val_folder))\n",
    "                val_num = val_num + 1\n",
    "            else:\n",
    "                copy2(src_img_path, test_folder)\n",
    "                # print(\"{}复制到了{}\".format(src_img_path, test_folder))\n",
    "                test_num = test_num + 1\n",
    "\n",
    "            current_idx = current_idx + 1\n",
    "\n",
    "        print(\"*********************************{}*************************************\".format(class_name))\n",
    "        print(\n",
    "            \"{}类按照{}：{}：{}的比例划分完成，一共{}张图片\".format(class_name, train_scale, val_scale, test_scale, current_data_length))\n",
    "        print(\"训练集{}：{}张\".format(train_folder, train_num))\n",
    "        print(\"验证集{}：{}张\".format(val_folder, val_num))\n",
    "        print(\"测试集{}：{}张\".format(test_folder, test_num))\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    src_data_folder = \"../dataset/data\"\n",
    "    target_data_folder = \"../dataset/CornDataset/data\"\n",
    "    data_set_split(src_data_folder, target_data_folder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "536b6965-5158-4328-a1de-8d2d99712716",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
